package service

import (
	"context"
	"gitee.com/hxchjm/go-admin/internal/dao"
	"gitee.com/hxchjm/go-admin/internal/models"
	"gitee.com/hxchjm/go-admin/pkg/jwtauth"
	"github.com/jinzhu/copier"
)

type Post struct {
}

// GetPage
// @Summary 岗位列表数据
// @Description 获取JSON
// @Tags 岗位
// @Param postName query string false "postName"
// @Param postCode query string false "postCode"
// @Param postId query string false "postId"
// @Param status query string false "status"
// @Success 200 {object} response.Response "{"code": 200, "data": [...]}"
// @Router /api/v1/post [get]
// @Security Bearer
func (e Post) GetPage(ctx context.Context, req *models.ReqPostPage) (any, error) {
	list, total, err := (dao.SysPost{}).GetPage(ctx, req)
	if err != nil {
		return nil, err
	}
	var ls []models.SysPost
	_ = copier.Copy(&ls, list)
	rsp := &models.RspPostPage{
		Page:  req.Page,
		Size:  req.Size,
		Total: int(total),
		List:  ls,
	}
	return rsp, nil
}

// Get
// @Summary 获取岗位信息
// @Description 获取JSON
// @Tags 岗位
// @Param id path int true "编码"
// @Success 200 {object} response.Response "{"code": 200, "data": [...]}"
// @Router /api/v1/post/{postId} [get]
// @Security Bearer
func (e Post) Get(ctx context.Context, req *models.ReqPostGet) (any, error) {
	return dao.SysPost{SysPost: models.SysPost{PostId: req.Id}}.Get(ctx)
}

// Insert
// @Summary 添加岗位
// @Description 获取JSON
// @Tags 岗位
// @Accept  application/json
// @Product application/json
// @Param data body dto.SysPostInsertReq true "data"
// @Success 200 {object} response.Response "{"code": 200, "data": [...]}"
// @Router /api/v1/post [post]
// @Security Bearer
func (e Post) Insert(ctx context.Context, req *models.ReqPostInsert) (any, error) {
	req.CreateBy = jwtauth.GetClaims(ctx).UID
	var sd = &dao.SysPost{}
	_ = copier.Copy(&sd, req)
	if err := sd.Insert(ctx); err != nil {
		return nil, err
	}
	return sd.PostId, nil
}

// Update
// @Summary 修改岗位
// @Description 获取JSON
// @Tags 岗位
// @Accept  application/json
// @Product application/json
// @Param data body dto.SysPostUpdateReq true "body"
// @Success 200 {object} response.Response "{"code": 200, "data": [...]}"
// @Router /api/v1/post/{id} [put]
// @Security Bearer
func (e Post) Update(ctx context.Context, req *models.ReqPostUpdate) (any, error) {
	req.UpdateBy = jwtauth.GetClaims(ctx).UID
	var sd = &dao.SysPost{}
	_ = copier.Copy(&sd, req)
	if err := sd.Update(ctx); err != nil {
		return nil, err
	}
	return sd.PostId, nil
}

// Delete
// @Summary 删除岗位
// @Description 删除数据
// @Tags 岗位
// @Param id body dto.SysPostDeleteReq true "请求参数"
// @Success 200 {object} response.Response "{"code": 200, "data": [...]}"
// @Router /api/v1/post [delete]
// @Security Bearer
func (e Post) Delete(ctx context.Context, req *models.ReqPostDelete) (any, error) {
	req.UpdateBy = jwtauth.GetClaims(ctx).UID
	var sd = &dao.SysPost{SysPost: models.SysPost{PostId: req.Ids[0]}}
	if err := sd.Delete(ctx); err != nil {
		return nil, err
	}
	return sd.PostId, nil
}
